package xyz.suancaiyu.userservice.exception;

import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import xyz.suancaiyu.dto.RespDTO;
import xyz.suancaiyu.exception.CommonException;

import static org.springframework.http.HttpStatus.OK;

/**
 * @author puruidong
 * @version 2019/1/24 22:13
 */
@RestControllerAdvice
public class CommonExceptionHandler {


    @ExceptionHandler(CommonException.class)
    public ResponseEntity<RespDTO> handleException(Exception ex) {
        RespDTO resp = new RespDTO();
        CommonException commonException = (CommonException) ex;
        resp.code = commonException.getCode();
        resp.error = ((CommonException) ex).getMsg();
        return new ResponseEntity(resp, OK);
    }

}
